package com.bitejiuyeke.bitecommoncore.utils;

import io.micrometer.common.util.StringUtils;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

public class StringUtil {


    /**\
     * 判断是否匹配规则
     * @param pattern
     * @param url
     * @return
     */
    public static boolean isMatch(String pattern,String url){
        if(StringUtils.isEmpty(url)||StringUtils.isEmpty(pattern)){
            return false;
        }

        AntPathMatcher matcher=new AntPathMatcher();
        return matcher.match(pattern,url);

    }

    /**
     * 匹配其中一个规则
     * @param str
     * @param patterns
     * @return
     */
    public static boolean matches(String str, List<String> patterns){
        if(StringUtils.isEmpty(str)|| CollectionUtils.isEmpty(patterns)){
            return false;
        }

        for(String pattern:patterns){
            if(isMatch(pattern,str)) return true;
        }

        return false;

    }

}
